import torch
from typing import List, Dict, Optional
from src.config.settings import MODEL_CONFIG
from zhipuai import ZhipuAI

class ModelHelper:
    """模型辅助工具"""
    
    def __init__(self):
        self.client = ZhipuAI(api_key=MODEL_CONFIG["api_key"])
        self.model_name = MODEL_CONFIG["name"]
        
    async def generate(self, prompt: str) -> Optional[str]:
        """生成文本"""
        try:
            response = self.client.chat.completions.create(
                model=self.model_name,
                messages=[
                    {"role": "system", "content": "You are an expert academic researcher."},
                    {"role": "user", "content": prompt}
                ],
                temperature=MODEL_CONFIG["temperature"],
                max_tokens=MODEL_CONFIG["max_tokens"]
            )
            return response.choices[0].message.content
            
        except Exception as e:
            print(f"生成失败: {str(e)}")
            return None
            
    def format_prompt(self, template: str, **kwargs) -> str:
        """格式化提示词"""
        try:
            return template.format(**kwargs)
        except Exception as e:
            print(f"格式化提示词失败: {str(e)}")
            return template
